package net.wanho.study.dao.impl;

import net.wanho.study.dao.UserDao;
import net.wanho.study.entity.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Author：汤小洋
 * Date：2025-09-12 16:03
 * Description：<描述>
 */
public class UserDaoImpl implements UserDao {
    @Override
    public void insert(User user) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ums?useUnicode=true&characterEncoding=utf8", "root", "root");
            String sql = "insert into t_user(username,password,age) values(?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setInt(3, user.getAge());
            int num = ps.executeUpdate();
            System.out.println("受影响的行数：" + num);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
